package com.hcb.tonguetiphealth.widget.bookOnline;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.hcb.tonguetiphealth.adapter.DishListAdapter;
import com.hcb.tonguetiphealth.global.Global;
import com.hcb.tonguetiphealth.manager.JSONManager;
import com.hcb.tonguetiphealth.model.ResCate;
import com.hcb.tonguetiphealth.model.ResDish;
import com.hcb.tonguetiphealth.util.HttpUtils;
import com.hcb.tonguetiphealth.util.MessageWhat;
import com.hcb.tonguetiphealth.view.R;
import com.hcb.tonguetiphealth.widget.BottomWidget;
import com.hcb.tonguetiphealth.widget.GeneralWidget;

public class BookOnlineWidget extends GeneralWidget implements OnClickListener {
	private static final String TAG = "BookOnlineWidget";
	
	private Activity ctx;
	private Handler mHandler;
	private Button btDish, btPkg;
	private ListView dishPkgListView;
	private DishListAdapter dishAdapter;
//	private CateListAdapter  cateAdapter;
	private String userid;//商家id
	private List<ResDish>resDishList;
	private List<ResCate>resCateList;
	private LinearLayout llback;
	private int type = 0; //0表示商家菜品，1表示商家套餐
	private LinearLayout llNullCate;
	
	public BookOnlineWidget(Context context) {
		super(context);
		ctx = (Activity)context;
		inflate(context, R.layout.online_book, this); 
		bindView();
		bindEvent();
		userid = ctx.getIntent().getStringExtra("resId");
		new BottomWidget(ctx, this);
		setTopBg(0);
		getResDishs();
	}
	
	private void bindEvent() {

		dishPkgListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				ResDish dish = (ResDish) arg0.getAdapter().getItem(arg2);
				ctx.getIntent().putExtra("dishId", dish.getId());
				ctx.getIntent().putExtra("resId", userid);
				ctx.getIntent().putExtra("FromWidgetFlag", "onlineBook");
//				Global.resList = resList; 
				ctx.setContentView(new DishInfoWidget(ctx));
			}
		});
		
		 
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				switch(msg.what) {
				case MessageWhat.SHOW_DIALOG:
					if (type == 0) {
						showMessageDialog(ctx, "正在获取商家菜单...");
					}else {
						showMessageDialog(ctx, "正在获取商家套餐列表...");
					}
					break;
				case MessageWhat.DIMISS_DIALOG:
					if (type == 0 ) {
						dishAdapter = new DishListAdapter(ctx, resDishList);
						Global.resDishList = resDishList;
						dishPkgListView.setAdapter(dishAdapter);
					}else {
//						cateAdapter = new CateListAdapter(ctx, resCateList);
//						Global.resCateList = resCateList;
//						dishPkgListView.setAdapter(cateAdapter);
					}
					dismissMessageDialog();
					break;
				case MessageWhat.NETWORK_FAILD:
					try {
					  dismissMessageDialog();
					} catch (Exception e) {
						// TODO: handle exception
					}
					//Toast.makeText(ctx,  "网络有点不给力哦", Toast.LENGTH_LONG).show();
					showMessage(ctx, R.string.net_faild);
					break;
				}
			}
		};
 	
	}
	
	private void bindView() {
	
		llback = (LinearLayout)findViewById(R.id.bookonline_info_back);
		btDish = (Button)findViewById(R.id.online_menus_button);
		btPkg= (Button)findViewById(R.id.online_pkg_button);
		dishPkgListView = (ListView)findViewById(R.id.dishpkg_listview);
		llNullCate = (LinearLayout)findViewById(R.id.list_null_linearlayout);
		
		llback.setOnClickListener(this);
		btDish.setOnClickListener(this);
		btPkg.setOnClickListener(this);
		 
	}
	
	private void setTopBg(int type){
		switch (type) {
		case 0:
			btDish.setBackgroundResource(R.drawable.bookonline_top_button_active);
			btPkg.setBackgroundResource(R.drawable.bookonline_top_button);
			break;
		case 1:
			btDish.setBackgroundResource(R.drawable.bookonline_top_button);
			btPkg.setBackgroundResource(R.drawable.bookonline_top_button_active);
			break;
		default:
			break;
		}
	}
	
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.online_menus_button:
			if (dishPkgListView.getVisibility() == View.GONE || dishPkgListView.getVisibility() == View.INVISIBLE ) {
				dishPkgListView.setVisibility(View.VISIBLE);
				llNullCate.setVisibility(View.GONE);
			}
			setTopBg(0);
			if (resDishList.size()>0) {
				dishAdapter = new DishListAdapter(ctx, resDishList);
				dishPkgListView.setAdapter(dishAdapter);
			}else {
				getResDishs();
			}
			break;
		case R.id.online_pkg_button:
			//getResPkg();
			dishPkgListView.setVisibility(View.GONE);
			llNullCate.setVisibility(View.VISIBLE);
			setTopBg(1);
			break;
		case R.id.bookonline_info_back:
			ctx.finish();
			break;
//		case R.id.onlinebook_change_button:
//			break;
		default:
			break;
		}
	}
 
	private void getResDishs(){
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		resDishList = new ArrayList<ResDish>();
		new Thread(){
			public void run() {
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("userid",userid));
				String result  = HttpUtils.Post(Global.getServletresdishs(), params);
				if (Global.DEBUG) {
					System.out.println("result:"+result);
				}
				JSONManager jsonManager = new JSONManager();
				boolean success = jsonManager.isGotJSONObject(result);
				if (success) {
					JSONArray jsonArray = jsonManager.getJsonList();
					for(int i=0; jsonArray != null  && i < jsonArray.length(); i++) {
						try {
							ResDish resDish = new ResDish();
							JSONObject obj = jsonArray.getJSONObject(i);
							resDish.setId(obj.getString("itemid"));
							resDish.setTitle(obj.getString("title"));
							resDish.setDishThumb(obj.getString("dish_thumb"));
							resDish.setNum(obj.getString("num"));
							resDish.setPrice(obj.getString("price"));
							resDishList.add(resDish);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
		
	}
	
	private void getResPkg(){
		mHandler.sendEmptyMessage(MessageWhat.SHOW_DIALOG);
		resDishList = new ArrayList<ResDish>();
		new Thread(){
			public void run() {
				List <NameValuePair> params = new ArrayList <NameValuePair>();
				params.add(new BasicNameValuePair("userid",userid));
				String result  = HttpUtils.Post(Global.getServletresdishs(), params);
				if (Global.DEBUG) {
					System.out.println("result:"+result);
				}
				JSONManager jsonManager = new JSONManager();
				boolean success = jsonManager.isGotJSONObject(result);
				if (success) {
					JSONArray jsonArray = jsonManager.getJsonList();
					for(int i=0; jsonArray != null  && i < jsonArray.length() && i < 10; i++) {//获取前10个
						try {
							ResDish resDish = new ResDish();
							JSONObject obj = jsonArray.getJSONObject(i);
							resDish.setId(obj.getString("itemid"));
							resDish.setTitle(obj.getString("title"));
							resDish.setDishThumb(obj.getString("dish_thumb"));
							resDish.setNum(obj.getString("num"));
							resDish.setPrice(obj.getString("price"));
							resDishList.add(resDish);
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
				mHandler.sendEmptyMessage(MessageWhat.DIMISS_DIALOG);
			}
		}.start();
		
	}
	
	@Override
	public void notifyAdapter() {
		
	}
 
	
}
